<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>
    <div class="layui-form layuimini-form" lay-filter="editForm">
        <div class="layui-form-item" hidden>
            <label class="layui-form-label required">职位ID</label>
            <div class="layui-input-block">
                <input type="text" name="positionId" lay-verify="required" lay-reqtext="职位ID不能为空" placeholder="请输入职位ID"
                    value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">职位名称</label>
            <div class="layui-input-block">
                <input type="text" name="positionName" placeholder="请输入职位名称" value="" class="layui-input"
                    lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">部门</label>
            <div class="layui-input-block">
                <input type="text" name="department" placeholder="请输入部门" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea name="description" placeholder="请输入描述" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>

    <script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer,
                $ = layui.$;
            //接参，加载数据
            form.getFormVal = function (rowdata) {
                //可以写其他业务请求。
                var data1 = JSON.parse(rowdata);
                // console.log(data1);
                // 数据回填
                form.val("editForm", data1);


                // 图片回显
                // 检查输入框是否有值
                var imageUrl = $('#avatarUpload').val();
                if (imageUrl) {
                    // 如果有值，将其回填到<img>的src属性中
                    $('#avatarPreview').attr('src', imageUrl);
                }
            }

            // 监听提交
            form.on('submit(saveBtn)', function (data) {
                console.log(JSON.stringify(data.field))
                // 模拟提交数据到后端
                $.ajax({
                    type: 'POST',
                    url: 'http://localhost:8080/api/job_position?operation=update', // 替换成实际的后端接口地址
                    data: JSON.stringify(data.field),
                    contentType: 'application/json',
                    success: function (result) {
                        if (result.code == 0) {
                            // 在父窗口中显示提示消息
                            parent.layer.msg('保存成功', { icon: 1, time: 2000 }); // 提示消息显示2秒
                            // 延迟关闭子窗口，确保提示消息显示完毕
                            setTimeout(function () {
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                                // 刷新父页面
                                parent.location.reload();
                            }, 300);
                        } else {
                            layer.msg('保存失败', { icon: 2 });
                        }
                    },
                    error: function () {
                        layer.msg('请求失败', { icon: 2 });
                    }
                });

                return false;
            });


            var upload = layui.upload;

            // 监听上传按钮的点击事件
            $('#uploadAvatarBtn').on('click', function () {
                $('#avatarUpload').click(); // 触发文件选择框的点击事件
            });

            // 上传头像
            upload.render({
                elem: '#avatarUpload',
                url: 'http://localhost:8080/file/upload', // 替换成实际的上传接口地址
                accept: 'images', // 只允许上传图片
                exts: 'jpg|png|gif|bmp|jpeg',
                done: function (res) {
                    // 直接使用后端返回的 URL 来更新头像预览
                    if (res.code === 1 && res.data.length > 0) {
                        var imageUrl = res.data[0];
                        $('#avatarPreview').attr('src', imageUrl);
                        // 更新隐藏的输入框的值
                        $('#avatarUpload').val(imageUrl);
                    } else {
                        layer.msg('上传失败', { icon: 2 });
                    }
                }
            });



        });
    </script>
</body>

</html>